package com.prs.crm.job.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.domain.cqunicom.OrderChartByMonth;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.job.ByMonTask;
import com.prs.crm.service.cqunicom.OrderChartByMonthService;
import com.prs.crm.service.cqunicom.OrderService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-4-8 下午04:16:48
 * 简单说明
 */

public class ByMonTaskImpl implements ByMonTask {
	
	
	private  OrderService orderService;
	
	@Autowired
	private  OrderChartByMonthService orderChartByMonthService;

    public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public OrderChartByMonthService getOrderChartByMonthService() {
		return orderChartByMonthService;
	}
	public void setOrderChartByMonthService(
			OrderChartByMonthService orderChartByMonthService) {
		this.orderChartByMonthService = orderChartByMonthService;
	}
	/**
     * 按照月份
     */
	public void byMon() {
		
		try {
			setByMonth();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	// 按月加入
	public void setByMonth() throws ParseException{
		 String  currens=setTimeStyleB().format(new Date());
		 Calendar c = Calendar.getInstance();
		  c.get(Calendar.MONTH);
		  c.roll(Calendar.MONTH, 1);// 切换到下一月
		  c.set(Calendar.DATE, 1);
		  String ends=setTimeStyleB().format(c.getTime())+" 00:00:00";
	     List<PoplarOrder>  orList=this.getOrderService().getCurrentOrder(setTimeStyleA().parse(currens+" 00:00:00"),setTimeStyleA().parse(ends));	
	     int serchLength=orList.size();
	
			List<OrderChartByMonth>  chartListzj=new ArrayList<OrderChartByMonth>();
			List<OrderChartByMonth>  chartListpd=new ArrayList<OrderChartByMonth>();
			if(serchLength>0){
			
			for(PoplarOrder order:orList){
				OrderChartByMonth oc=new OrderChartByMonth();
				
				 oc.setMonDate(setTimeStyleB().format(order.getRecordTime())+"");
				 oc.setMonHy(order.getServices().getName());
				 oc.setMonWord(order.getServices().getServicesType().getName());
				 oc.setMonBoNum(1);
				
				 if(order.getBusinessOrder()!=null){
					 if("已成交".equals(order.getBusinessOrder().getTransationSatet())){
						 oc.setMonCJnNum(1);
						 if(order.getMonitorOrder()!=null){
							 if(order.getMonitorOrder().getTradeAmount()!=null || !"".equals(order.getMonitorOrder().getTradeAmount())){
								 oc.setMonAllMonly(order.getMonitorOrder().getTradeAmount());
							 }else{
								 oc.setMonAllMonly(0.0);
							 }
						 }else{
							 oc.setMonAllMonly(0.0);
						 }
						 
					 }else{
						 oc.setMonAllMonly(0.0);
						 oc.setMonCJnNum(0);
					 }
				 }else{
					  oc.setMonCJnNum(0);
				 }			
				 oc.setMonCJlv("0%");
				 if("转接".equals(order.getModleName())){
						oc.setModles("转接");
						 oc.setMonSJBoNum(1);
						chartListzj.add(oc);
					}else{
						oc.setModles("派单");
						 oc.setMonSJBoNum(0);
						chartListpd.add(oc);
					}			
			    }
			
			}
			//派单处理
//			chartList
			List<OrderChartByMonth>  chartListpd2=new ArrayList<OrderChartByMonth>();
			for(OrderChartByMonth pd:chartListpd){
				if(chartListpd2.size()==0){
					chartListpd2.add(pd);	
				}else{
					int j=0;
					for(int i=0;i<chartListpd2.size();i++){
						String sera=pd.getMonHy();
						String serb=chartListpd2.get(i).getMonHy();
						
						if(sera.equals(serb)){
							chartListpd2.get(i).setMonBoNum(chartListpd2.get(i).getMonBoNum()+pd.getMonBoNum());
							chartListpd2.get(i).setMonCJnNum(chartListpd2.get(i).getMonCJnNum()+pd.getMonCJnNum());
							if(chartListpd2.get(i).getMonAllMonly()==null && pd.getMonAllMonly()==null){
								chartListpd2.get(i).setMonAllMonly(0.0);
							}else if(chartListpd2.get(i).getMonAllMonly()==null && pd.getMonAllMonly()!=null){
								chartListpd2.get(i).setMonAllMonly(pd.getMonAllMonly());
							}else if(chartListpd2.get(i).getMonAllMonly()!=null && pd.getMonAllMonly()==null){
								chartListpd2.get(i).setMonAllMonly(chartListpd2.get(i).getMonAllMonly());
							}else{
							chartListpd2.get(i).setMonAllMonly(chartListpd2.get(i).getMonAllMonly()+pd.getMonAllMonly());
							}
						    j++;
						}
					}
					    if(j>0){
					    	
					    }else{
						chartListpd2.add(pd);
					    }
				}	
			}
			//转接处理
			List<OrderChartByMonth>  chartListpd1=new ArrayList<OrderChartByMonth>();
			for(OrderChartByMonth pd:chartListzj){
				if(chartListpd1.size()==0){
					chartListpd1.add(pd);	
				}else{
					int j=0;
					for(int i=0;i<chartListpd1.size();i++){
						OrderChartByMonth ss=chartListpd1.get(i);
						String sera=pd.getMonHy();
						String serb=ss.getMonHy();
						if(sera.equals(serb)){
							ss.setMonBoNum(ss.getMonBoNum()+pd.getMonBoNum());
							ss.setMonCJnNum(ss.getMonCJnNum()+pd.getMonCJnNum());
							j++;
						}
					}
					if(j==0){

							chartListpd1.add(pd);
						
					}
				}	
			}
			//总和处理
			List<OrderChartByMonth>  chartListpdPage=new ArrayList<OrderChartByMonth>();//最终存放list
			//本月总的派单金额
			double allmonly=0.0;
			for(OrderChartByMonth OO:chartListpd2){		
				
				if(OO.getMonAllMonly()==null){
					
				}else{
					allmonly=allmonly+OO.getMonAllMonly();
				}
				
			}
			
			for(OrderChartByMonth ss:chartListpd2){
				if(ss.getMonBoNum()==0){
					ss.setMonCJlv("0%");	
				}else{
				ss.setMonCJlv((ss.getMonCJnNum()/ss.getMonBoNum())*100+"%");
				}
				 if(ss.getMonAllMonly()==null){
					 ss.setMonAc("0%");
				 }else{
					 ss.setMonAc((ss.getMonAllMonly()/allmonly)*100+"%");
				 }
				//上月对象
					OrderChartByMonth  UpObject=this.getOrderChartByMonthService().getByTimeSerNames(setTimeStyleB().format(new Date()), ss.getMonHy(), ss.getMonWord());
				    if(UpObject==null){
				    ss.setMonAdd("上升:"+ss.getMonCJnNum()*100+"%");	
				    }else{
				    int cz=	ss.getMonCJnNum()-UpObject.getMonCJnNum();
				    if( cz > 0 ){
				    	ss.setMonAdd("上升:"+cz*100+"%");
				    }else{
				    	ss.setMonAdd("下降:"+(-cz*100)+"%");
				    }	
				    	
				    }
					chartListpdPage.add(ss);	
			}
			for(OrderChartByMonth ss:chartListpd1){
				chartListpdPage.add(ss);
			}
	     for(OrderChartByMonth oc:chartListpdPage){
	    	
	    	 this.getOrderChartByMonthService().save(oc);
	     }
	     
	
	     
	}
	public static SimpleDateFormat setTimeStyleA() {
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return ddf;
	}

	public static SimpleDateFormat setTimeStyleB() {
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd");
		return ddf;
	}
}
